package com.zhongxi.guoyuan.view;

import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.TipsAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.TipsBean;
import com.zhongxi.guoyuan.entity.TipsDetailBean;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class NewTipsActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private NoScrollListView lvList;
    private List<TipsBean.TipsData> data = new ArrayList<>();
    private TipsAdapter adapter;
    private int pos;
    private String type;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_new_tips);
        initTitle();
        tvTitle.setText("新手指南");
    }

    @Override
    protected void initData() {
        super.initData();
        lvList = (NoScrollListView) findViewById(R.id.lvList);
        getData();
    }

    private void getData() {
        type = "list";
        String str = "&page="+pageNum;
        getServer(Url.NOVICE_MANUALLIST, null, str, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) throws JSONException {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("list".equals(type)) {
                TipsBean result = gson.fromJson(response, TipsBean.class);
                if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
                    MyApplication.getInstance().setSpValue(result.getSafeobj());
                }
                if (null != result.getData() && result.getData().size() > 0) {
                    data.addAll(result.getData());
                }
                adapter = new TipsAdapter(context, data);
                lvList.setAdapter(adapter);
            }else if("info".equals(type)){
                TipsDetailBean result = gson.fromJson(response, TipsDetailBean.class);
                if(null!=result.getData()&&!"".equals(result.getData())){
                    if (pos == adapter.getmExpandedMenuPos()) {
                        adapter.setmExpandedMenuPos(-1, result.getData());
                    } else {
                        adapter.setmExpandedMenuPos(pos, result.getData());
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(adapter.getmExpandedMenuPos()==-1){
            getInfo(position);
        }else{
            if(position==adapter.getmExpandedMenuPos()){
                adapter.setmExpandedMenuPos(-1);
                adapter.notifyDataSetChanged();
            }else{
                getInfo(position);
            }
        }
    }

    /**
     * 获取展开详细信息
     * @param position
     */
    private void getInfo(int position) {
        pos = position;
        type = "info";
        String str = "&nmid="+data.get(position).getId();
        getServer(Url.NOVICE_MANUAL_SHOW, null, str, false, null, "get");
    }
}
